package com.holy.sh.couviewholy.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.holy.sh.couviewholy.R;

/**
 * Holy Email :victorhhl@163.com
 * Create by Holy on ${data}
 * Description :
 */
public class DontaiBitMapView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
    private SurfaceHolder sfh;
    private Paint paint;
    private Thread th;
    private boolean flag;
    private boolean isMove=false;
    private Canvas canvas;
    //声明一张波浪位图
    private Bitmap bmp;
    //声明波浪图的X,Y坐标
    private int bmpX, bmpY;

    public DontaiBitMapView(Context context) {
        super(context);
        sfh = this.getHolder();
        sfh.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        setFocusable(true);
        Log.i("111","1");
    }

    public DontaiBitMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        sfh = this.getHolder();
        sfh.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        setFocusable(true);
        Log.i("111","2");
    }

    public DontaiBitMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        sfh = this.getHolder();
        sfh.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        setFocusable(true);
        Log.i("111","");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        bmp = BitmapFactory.decodeResource(this.getResources(), R.mipmap.water);
        //让位图初始化X坐标正好充满屏幕
        bmpX = -bmp.getWidth()+this.getWidth();
        //让位图绘制在的最下方，且图片Y坐标正好是（屏幕高-图片高）
        bmpY = this.getHeight() - bmp.getHeight();
        //实例线程
        th = new Thread(this);
        flag = true;
        //启动线程
        th.start();
        myDraw();
    }

    /**
     * 游戏绘图
     */
    public void myDraw() {
        try {
            canvas = sfh.lockCanvas();
            if (canvas != null) {
                canvas.drawColor(Color.WHITE);
                canvas.drawBitmap(bmp, bmpX, bmpY, new Paint());
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (canvas != null)
                sfh.unlockCanvasAndPost(canvas);
        }
    }


    /**
     * 触屏事件监听
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if (isMove) {
                    isMove = false;
                } else {
                    isMove = true;
                }
                break;
        }
        return true;
    }

    /**
     * 按键事件监听
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 游戏逻辑
     */
    private void logic() {
        bmpX+=5;
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        flag = false;
    }

    @Override
    public void run() {
        while (flag) {
            long start = System.currentTimeMillis();
            if (isMove){
                myDraw();
                logic();
            }
            long end = System.currentTimeMillis();
            try {
                if (end - start < 50) {
                    Thread.sleep(50 - (end - start));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
